package com.example.springbott.controller;

import com.example.springbott.aspactJ.OperationName;
import com.example.springbott.bean.BaseRespVo;
import com.example.springbott.bean.MarketStorage;
import com.example.springbott.bean.common.CommonData;
import com.example.springbott.bean.common.PageInfoBase;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


/**
 * 存储控制器
 *
 * @author kerbal
 * @date 2022/11/22
 */
@RestControllerAdvice
@RequestMapping("admin/storage")
public class StorageController {
    @Autowired

    com.example.springbott.service.adminStorageService adminStorageService;

    /**
     * 管理存储列表
     * http://182.92.235.201:8083/admin/storage/list?page=1&limit=20&key=myuuhvory8piis8dkxq6.jpg&name=previewFix.jpg&sort=add_time&order=desc
     *
     * @return {@link BaseRespVo}
     * @kerbal
     */
    @RequiresPermissions("admin:storage:list")
    @RequestMapping("list")
    @OperationName("1")
    public BaseRespVo adminStorageList(PageInfoBase pageInfoBase, String key, String name) {
        CommonData commonData = adminStorageService.adminStorageList(pageInfoBase, key, name);
        return BaseRespVo.ok(commonData);
    }


    /**
     * 管理存储创建
     *
     * @param request 请求
     * @param file    文件
     * @return {@link BaseRespVo}
     * @throws IOException ioexception
     * @kerbal http://localhost:8083/admin/storage/create
     */

    @OperationName("1")
    @RequiresPermissions("admin:storage:create")
    @RequestMapping("create")
    public BaseRespVo adminStorageCreate(HttpServletRequest request, MultipartFile file) throws IOException {
        MarketStorage marketStorage = adminStorageService.adminStorageCreate(request, file);
        return BaseRespVo.ok(marketStorage);

    }



    /**
     * 管理存储更新
     *http://localhost:8083/admin/storage/update
     * @param marketStorage 市场存储
     * @return {@link BaseRespVo}
     */
    @RequiresPermissions("admin:storage:update")
    @RequestMapping("update")
    @OperationName("1")
    public BaseRespVo adminStorageUpdate(@RequestBody MarketStorage marketStorage) {
        MarketStorage marketStorage1 = adminStorageService.adminStorageUpdate(marketStorage);
        return BaseRespVo.ok(marketStorage1);
    }

    @RequiresPermissions("admin:storage:delete")
    @RequestMapping("delete")
    @OperationName("1")
    public BaseRespVo adminStorageDelete(@RequestBody MarketStorage marketStorage){
        Integer code = adminStorageService.adminStorageDelete(marketStorage);
        if (code==1){
           return BaseRespVo.ok(null);
        }else {
        return     BaseRespVo.invalidData("删除失败");
        }


    }
}
